package me.helllp.dake.iam.core.logic.auth.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import me.helllp.dake.common.exception.DakeException;
import me.helllp.dake.core.bean.DakeAuthAccount;
import me.helllp.dake.iam.core.bean.DakeAuthTicket;
import me.helllp.dake.iam.core.bo.UserRoleAndAppBo;
import me.helllp.dake.iam.core.logic.auth.ITokenLogic;
import me.helllp.dake.iam.core.logic.auth.IAccountLogic;
import me.helllp.dake.iam.core.mapper.DakeApplicationMapper;
import me.helllp.dake.iam.core.mapper.DakeTenantMapper;
import me.helllp.dake.iam.core.po.DakeAccount;
import me.helllp.dake.iam.core.dao.IDakeAccountService;
import me.helllp.dake.iam.core.mapper.DakeAccountMapper;
import me.helllp.dake.iam.core.mapper.DakeAccountRoleRelMapper;
import org.apache.commons.lang3.Validate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class DefaultAccountLogic implements IAccountLogic {
    @Autowired
    private DakeAccountMapper accountMapper;

    @Autowired
    private DakeApplicationMapper applicationMapper;

    @Autowired
    private DakeAccountRoleRelMapper accountRoleRelMapper;

    @Autowired
    private DakeTenantMapper tenantMapper;

    @Autowired
    private IDakeAccountService accountService;

    @Autowired
    private ITokenLogic tokenService;

    public DakeAuthAccount transferToAuthUser(Long accountId) {
        Validate.notNull(accountId);
        DakeAuthAccount authUser = accountMapper.queryAuthAccount(accountId);
        Assert.notNull(authUser, "当前账号不存在");

        // 设置角色信息
        List<UserRoleAndAppBo> appRoles = accountRoleRelMapper.queryRoleAndApp(accountId, authUser.getTenantId());
        if (CollectionUtils.isNotEmpty(appRoles)) {
            Map<String, Set<String>> roleMaps = new HashMap<>();

            Map<String, List<UserRoleAndAppBo>> maps = appRoles.stream().collect(Collectors.groupingBy(UserRoleAndAppBo::getAppCode));
            maps.forEach((key, value) -> {
                roleMaps.put(key, value.stream().map(UserRoleAndAppBo::getRoleName).collect(Collectors.toSet()));
            });
            authUser.setRoles(roleMaps);
        }
        return authUser;
    }

    @Override
    public DakeAuthTicket login(String account, String password, Long tenantId) throws DakeException {
        QueryWrapper<DakeAccount> wrapper = new QueryWrapper();
        wrapper.eq("ACCOUNT", account).eq("TENANT_ID", tenantId);

        DakeAccount info = accountMapper.selectOne(wrapper);
        if (!accountService.checkPassword(info, password)) {
            throw new DakeException();
        }

        DakeAuthTicket ticket = new DakeAuthTicket();
        ticket.setAuthToken(tokenService.createToken());
        ticket.setAuthUser(transferToAuthUser(info.getId()));
        return ticket;
    }
}
